/**
 * 分割URL地址
 * 把地址参数整合成对象
 */
function urlSplit(url) {
    let params = url.split("?");
    let obj = {};
    for (let i = 1,len = params.length; i < len; i++) {
        let str = params[i];                
        if(str.indexOf("#") > 0){
            str = str.split("#")[0];
        }
        if(str.indexOf("&") > 0){
            let result = str.split("&");
            for(let j of result){
                obj[`${j.split("=")[0]}`] = j.split("=")[1];
            }
        }else{
            obj[`${str.split("=")[0]}`] = str.split("=")[1];
        }
    }
    return obj;
}

/*
* 调用方法
* JS获取视频第一帧作为封面图
* let videoUrl = 'https://fld-wenzhi-1305125648.cos.ap-guangzhou.myqcloud.com/file/2.mp4';
* cutVideoPicture(videoUrl).then(res => {
*     console.log(res)
* });
**/
function cutVideoPicture(videoUrl) {
    return new Promise(function (resolve) {
        let imgUrl = '';
        let video = document.createElement("video");
        video.style = "position:fixed; top: 9999px;left:9999px;z-index:-9999";
        video.preload = "metadata";
        video.currentTime = 1; //截取的视频第一秒作为图片
        video.src = videoUrl;
        video.setAttribute("crossOrigin", "anonymous");
        video.width = 400;
        video.height = 300;
        document.body.appendChild(video);
        video.onloadeddata = function () {
            let canvas = document.createElement("canvas");
            canvas.width = video.width;
            canvas.height = video.height;
            canvas.getContext("2d").drawImage(video, 0, 0, video.clientWidth, video.clientHeight);
            //这里输出的oGrayImg就是视频的封面图
            var oGrayImg = canvas.toDataURL("image/jpeg");
            resolve(oGrayImg);
            this.remove();
        };
    })
}

